#!/usr/bin/env python

import wx
import wx.aui
import os.path
import __main__

from utils import tracer
from utils.eventHandler import EventHandler, EventArgs

class MDIFrame(wx.Frame):
  PLUGIN_PATH = 'plugins'
  CONFIG_PATH = 'config'
  
  def __init__(self, caption, position=wx.DefaultPosition,
               windowsize=wx.DefaultSize):
    wx.Frame.__init__(self, None, -1,
                      title=caption,
                      pos=position,
                      size=windowsize,
                      style=wx.DEFAULT_FRAME_STYLE |
                      wx.SUNKEN_BORDER | wx.CLIP_CHILDREN)

    self._auiMgr = None
    self._uiManagers = None
    self._pluginMgr = None

    # event handlers
    self._auiPaneCloseEventHandler = EventHandler()
    self.Bind(wx.aui.EVT_AUI_PANE_CLOSE, self.onAuiPaneClose)

    # bind events
    self.Bind(wx.EVT_CLOSE, self.onClose)

  def auiPaneCloseEventHandler(self, handler):
    self._auiPaneCloseEventHandler.add(handler)
  def onAuiPaneClose(self, evt):
    eventArgs = EventArgs((evt,))
    self._auiPaneCloseEventHandler.invoke(self, eventArgs)

  def getAuiManager(self):
    if self._auiMgr is None:
      self._auiMgr = wx.aui.AuiManager(self)
      self._auiMgr.Update()
    return self._auiMgr

  def addUIManager(self, uiMgr):
    if uiMgr is None:
      return 

    if self._uiManagers is None:
      self._uiManagers = {}

    tracer.writeln('Adding UIManager ...')

    name = uiMgr.getName()
    if self._uiManagers.has_key(name):
      return False

    self._uiManagers[name] = uiMgr
    if not self._pluginMgr is None:
      uiMgr.load(self._pluginMgr)
      uiMgr.attach(self)

    tracer.writeln('UIManager %s had been added.%s' % (name, os.linesep))

    return True

  def getUIManagers(self):
    if self._uiManagers is None:
      return None
    return self._uiManagers.items()

  def getUIManagerByName(self, name):
    if not self._uiManagers.has_key(name):
      return None
    return self._uiManagers[name]

  def setPluginManager(self, pluginManager):
    if not self._pluginMgr is None and (pluginManager is None or pluginManager != self._pluginMgr):
      # unload UIs
      tracer.writeln('Unloading and detaching sub UIs from frame ...')
      for uiMgr in self._uiManagers.values():
        uiMgr.unload()
        uiMgr.detach()

      # uninit modes
      tracer.writeln('Uninitializing plugins ...')
      plugins = self._pluginMgr.getPlugins()
      for plugin in plugins:
        mod = plugin.getModule()
        if mod is None:
          continue
        mod.uninit()
  
      tracer.writeln('Remove plugin manager ...')
      self._pluginMgr = None     

      tracer.writeln('All done.%s' % os.linesep)

    if not pluginManager is None:
      tracer.writeln('Setting plugin manager ...')

      self._pluginMgr = pluginManager
      self._pluginMgr.setOwner(self)

      tracer.writeln('Initializing plugins ...')
      # init mods
      plugins = self._pluginMgr.getPlugins()
      for plugin in plugins:
        mod = plugin.getModule()
        if mod is None:
          continue
        mod.init(self, self._pluginMgr)

      # reload UIs
      tracer.writeln('Loading and attaching sub UIs from plugins ...')
      if self._uiManagers is not None:
        for uiMgr in self._uiManagers.values():
          uiMgr.load(self._pluginMgr)
          uiMgr.attach(self)

      tracer.writeln('All done.%s' % os.linesep) 

  def onClose(self, evt):
    if not self._pluginMgr is None:
      self.setPluginManager(None)
    self.Destroy()

  def onPerferences(self, evt):
    pass

  def onExit(self, evt):
    self.onClose(evt)

  def onAbout(self, evt):
    pass

##########################################################################
# test codes
class TestApp(wx.App):
  def __init__(self):
    wx.App.__init__(self)
    frame = MDIFrame('Test MDI Parent Frame')
    frame.Show(True)
    self.SetTopWindow(frame)

  def start(self):
    self.MainLoop()
    
if __name__ == '__main__':
  app = TestApp()
  app.start()
